﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Wormhole.UI.Application.Controls.Power
{
	/// <author>Rodney McKay</author>
	/// <date>02 April 2008</date>
	/// <summary>
	/// Display the mission total that the player reached so far by assigning characters.
	/// </summary>
	public partial class MissionTotal : UserControl
	{
		#region Properties

		#region Game
	
		private Engine.Game _game;
		/// <summary>
		/// The Game engine object.
		/// </summary>
		public Engine.Game Game
		{
			get
			{
				return this._game;
			}

			set
			{
				if (this._game != value)
				{
					this._game = value;
					this.SetEngineEvents();
				}
			}
		}
		#endregion

		#endregion

		#region Constructor
		/// <summary>
		/// Initialize a default instance of the class.
		/// </summary>
		public MissionTotal()
		{
			InitializeComponent();
			this.DisplayTotal(0);
		}

		#endregion

		#region Set Engine Events
		/// <summary>
		/// Wire up the events for the engine objects.
		/// </summary>
		protected void SetEngineEvents()
		{
			this.Game.Mission.TotalChanged += new Wormhole.Engine.Mission.Mission.TotalChangedDelegate(Mission_TotalChanged);
		}
		#endregion

		#region Event Handlers

		#region Total Changed
		/// <summary>
		/// The event handler for the total changed.
		/// </summary>
		private void Mission_TotalChanged(int total)
		{
			this.Dispatcher.Invoke(
				DispatcherPriority.Normal,
				(System.Windows.Forms.MethodInvoker)delegate()
			{
				this.DisplayTotal(total);
			});
		}
		#endregion

		#region Mouse Down
		/// <summary>
		/// Mouse Down Event Handler.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnMouseDown(MouseButtonEventArgs e)
		{
			if (e.LeftButton == MouseButtonState.Pressed)
				this.IncreaseTotal();
			else if (e.RightButton == MouseButtonState.Pressed)
				this.DecreaseTotal();

			base.OnMouseDown(e);
		}
		#endregion

		#endregion

		#region Display Total
		/// <summary>
		/// Display the mission total.
		/// </summary>
		/// <param name="total">The value to display</param>
		private void DisplayTotal(int total)
		{
			this.lblTotal.Content = total;
		}
		#endregion

		#region Increase Total
		/// <summary>
		/// Increase the mission total.
		/// </summary>
		protected void IncreaseTotal()
		{
			if (this.Game != null)
				this.Game.Mission.Total++;
		}
		#endregion

		#region Decrease Total
		/// <summary>
		/// Decrease the mission total.
		/// </summary>
		protected void DecreaseTotal()
		{
			if (this.Game != null)
				this.Game.Mission.Total--;
		}
		#endregion
	}
}
